package com.example.studentsystem;

import android.app.ActivityOptions;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.studentsystem.Bean.Student;
import com.example.studentsystem.db.DBHelper;
import com.example.studentsystem.db.MainInterface;


public class LoginActivity extends AppCompatActivity{
    private DBHelper DbHelper;
    private EditText mlogin_edit_account;
    private EditText mlogin_edit_pwd;
    private SharedPreferences mSharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Button button=(Button)findViewById(R.id.login_btn_forgetregister);
        mlogin_edit_account=findViewById(R.id.login_edit_account);
        mlogin_edit_pwd=findViewById(R.id.login_edit_pwd);
        mSharedPreferences = getSharedPreferences("user_data", MODE_PRIVATE);
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){new AlertDialog.Builder(LoginActivity.this).setTitle("系统提示").setMessage("请输入验证信息进行验证!")
                    .setPositiveButton("确定",new DialogInterface.OnClickListener(){
                        @Override
                        public void onClick(DialogInterface dialog,int which){
                            finish();
                        }
                    }).setNegativeButton("返回",new DialogInterface.OnClickListener(){
                        @Override
                        public void onClick(DialogInterface dialog,int which){
                        }

                    }).show();
            }
        });
        Button button1=(Button)findViewById(R.id.login_btn_login);
        button1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){

                String username = mlogin_edit_account.getText().toString();
                String password = mlogin_edit_pwd.getText().toString();
                if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
                    Toast.makeText(LoginActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                Student student = DbHelper.getInstance(LoginActivity.this).login(username, password);
                if (student == null) {
                    Toast.makeText(LoginActivity.this, "登陆失败", Toast.LENGTH_SHORT).show();
                }else{
                    putInt("student_id",student.getStudent_id());
                    Intent intent = new Intent(LoginActivity.this, MainInterface.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        });
        Button button2=(Button) findViewById(R.id.login_btn_register);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }
    private void putInt(String key, int value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putInt(key, value);
        editor.apply();
    }
}
